package com.tianhui.consignortwofei.pattern.orderOprate.impl;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.fgs.common.CommonModel;
import com.fgs.common.CommonResponse;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.utils.TipDialogUtil;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.tianhui.consignortwofei.api.OrderApiService;
import com.tianhui.consignortwofei.constant.OrderStatus;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.MainActivity;
import com.tianhui.consignortwofei.pattern.orderOprate.IOrderOperate;
import com.tianhui.consignortwofei.role.RoleController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/9/2
 * @description 终止单据(调度单)
 **/
public class TerminateDispatchOperate implements IOrderOperate {
    @Override
    public void orderOperate(Context context, OrderOperateInfo operateInfo) {

//        司机取消/运输/装车/收货/已计算/终止/已完成过程中不可进行终止!(state = 3 或 抢单15分钟之内)
        int status = operateInfo.orderInfo.isstate;
//        if (status == OrderStatus.CANCELING ||
//                status == OrderStatus.CANCELED ||
//                status == OrderStatus.IN_TRANSIT ||
//                status == OrderStatus.LOADING_CONFIRM ||
//                status == OrderStatus.DELIVER_CONFIRM ||
//                status == OrderStatus.CALCULATED ||
//                status == OrderStatus.TERMINATION ||
//                status == OrderStatus.PAYMENT ||
//                status == OrderStatus.COMPLETE
//        )

        boolean isCanTerminate = RoleController.getInstance().isCanTerminateDispatchOrder();
        if (isCanTerminate) {
            if (status == OrderStatus.TERMINATION) {
                TipDialogUtil.showTipDialog(context, "单据已经终止！");
                return;
            }
            showTerminateDialog(context, operateInfo);
        } else {
            if (operateInfo != null && operateInfo.orderInfo != null && !TextUtils.isEmpty(operateInfo.orderInfo.robbilltime)) {
                String rabTime = operateInfo.orderInfo.robbilltime;
                long rabLongTime = DateUtil.stringToLong(rabTime, "yyyy-MM-dd HH:mm:ss");
                long currentLongTime = System.currentTimeMillis();
                int time = (int) ((currentLongTime - rabLongTime) / 1000 / 60);

//                if (status == OrderStatus.TO_BE_LOADED) {// && time < 15
//                    if (time > 15) {
//                        TipDialogUtil.showTipDialog(context, "抢单超过15分钟不可取消");
//                    } else {
                        showTerminateDialog(context, operateInfo);
//                    }
//                } else {
//                    TipDialogUtil.showTipDialog(context, "司机取消/运输/装车/收货/已计算/终止/已完成过程中不可进行终止!");
//                }
            } else {
                ToastUtil.showShortToast("获取订单信息失败");
            }
        }


    }

    private void showTerminateDialog(Context context, OrderOperateInfo operateInfo) {
        new OperateTipDialog.Builder(context)
                .title("请输入原因")
                .input(new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
//                        if (TextUtils.isEmpty(reason)) {
//                            ToastUtil.showShortToast("请输入终止原因");
//                            return;
//                        }
                        dialog.dismiss();
                        Map<String, String> map = new HashMap<>();
                        map.put("billno", operateInfo.orderInfo.dispatchno);
                        map.put("remark", reason);
                        map.put("isstate", "11");
                        if (OrderType.TYPE_DISPATCH_DINGJIN==operateInfo.orderInfo.dingJinType) {//区分有无订金
                            new CommonModel().subscribe(context, CommonConfig.getInstance().create(OrderApiService.class).orderTerminateOperateDingJin(map), new ObserverResponseListener<CommonResponse>() {
                                @Override
                                public void onNext(CommonResponse commonResponse) {
                                    if (commonResponse.getCode() == 20000) {
                                        showTerminateResultDialog(context, "单据终止成功", operateInfo);
                                        EventBusManage.refreshOrderList();
                                        EventBusManage.activityFinish();
                                    } else {
                                        ToastUtil.showShortToast(commonResponse.getMsg());
                                    }
                                }

                                @Override
                                public void onError(ExceptionHandle.ResponseThrowable e) {
                                    ToastUtil.showShortToast(e.message);
                                }
                            });
                        }else {
                            new CommonModel().subscribe(context, CommonConfig.getInstance().create(OrderApiService.class).orderTerminateOperate(map), new ObserverResponseListener<CommonResponse>() {
                                @Override
                                public void onNext(CommonResponse commonResponse) {
                                    if (commonResponse.getCode() == 20000) {
                                        showTerminateResultDialog(context, "单据终止成功", operateInfo);
                                        EventBusManage.refreshOrderList();
                                        EventBusManage.activityFinish();
                                    } else {
                                        ToastUtil.showShortToast(commonResponse.getMsg());
                                    }
                                }

                                @Override
                                public void onError(ExceptionHandle.ResponseThrowable e) {
                                    ToastUtil.showShortToast(e.message);
                                }
                            });
                        }
                    }
                })
                .show();
    }

    private void showTerminateResultDialog(Context context, String message, OrderOperateInfo operateInfo) {
        new OperateTipDialog.Builder(context)
                .title("提示")
                .content(message)
                .isCancelable(false)
                .positiveText("返回列表")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        dialog.dismiss();
                        Intent intent = new Intent(context, MainActivity.class);
//                        Intent intent = new Intent(context, OrderListActivity.class);
//                        Bundle bundle = new Bundle();
//                        bundle.putInt(KeyValue.KEY_VALUE, operateInfo.type);
//                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                })
                .show();
    }
//    @Override
//    public void orderOperate(Context context, OrderOperateInfo operateInfo, OrderOperateCallback orderOperateCallback) {
//
//    }
}
